/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package net.wofly.sale.commodity.repository;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import net.wofly.sale.commodity.domain.Commodity;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface CommodityRepository
		extends PagingAndSortingRepository<Commodity, String>, JpaSpecificationExecutor<Commodity> {

	List<Commodity> findByCommodityCode(@Param("commodityCode") String commodityCode);
	
	List<Commodity> findByCommodityName(@Param("commodityName") String commodityName);

	List<Commodity> findByCommodityTypeIDAndStatus(@Param("commodityTypeID") String commodityTypeID, @Param("status") Integer status);

	List<Commodity> findByCommodityTypeID(@Param("commodityTypeID") String commodityTypeID);

	List<Commodity> findByStatus(@Param("status") Integer status);
	
	List<Commodity> getAllByStatus(Integer status) ;
	
	@Query(value = "select c from Commodity c where commodityTypeID like :commodityTypeID% and status = :status order by commodityCode desc")
	List<Commodity> findByCommodityTypeIDLikeAndStatus(@Param("commodityTypeID") String commodityTypeID, @Param("status") Integer status);

	@Query(value = "select c from Commodity c where commodityTypeID like :commodityTypeID% order by commodityCode desc")
	List<Commodity> findByCommodityTypeIDLike(@Param("commodityTypeID") String commodityTypeID);
	
	@Query("select max(c.commodityCode) from Commodity c")
	String findMaxCommodityCode();
	
}